package com.honkentuber.chat;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Index;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.repackaged.com.google.common.base.Preconditions;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
// @Unique(members={"userKey", "room"})
@Index(name="KEY_IDX", members={"key"})
public class Presence {
  @SuppressWarnings("unused")
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;

  @Persistent
  private Key userKey;

  @SuppressWarnings("unused")
  @Persistent
  private Room room;

  @Persistent
  private long lastAccessMs = 0;

  public Presence(Key userKey, Room room) {
    Preconditions.checkNotNull(userKey);
    Preconditions.checkNotNull(room);
    this.userKey = userKey;
    this.room = room;
  }

  public Key getUserKey() {
    return userKey;
  }

  public String toString() {
    return userKey + " at " + lastAccessMs;
  }

  public long getLastAccessMs() {
    return lastAccessMs;
  }

  // TODO(ptucker) add lastMessageMs

  public void setLastAccessMs(long lastAccessMs) {
    this.lastAccessMs = lastAccessMs;
  }
}